#include "notepad.h"
#include "ui_notepad.h"

#include <QTextEdit>
#include <QString>
#include <QMessageBox>
#include <QFileDialog>
#include <QTextStream>
#include <QFile>
#include <QCloseEvent>
#include <QDebug>
notepad::notepad(QWidget *parent) : QMainWindow(parent),
                                    ui(new Ui::notepad)
{
    ui->setupUi(this);
    setWindowTitle("记事本");
    resize(800, 800);
    // 调整文本框
    textEdit = new QTextEdit(this);
    textEdit->setFont(QFont("微软雅黑", 12));
    setCentralWidget(textEdit); // 居中
    // 创建菜单栏，只能有一个
    QMenuBar *bar = menuBar();
    // 将菜单栏放入窗口中
    setMenuBar(bar);
    // 创建菜单
    QMenu *fileMenu = bar->addMenu("文件");
    QMenu *editMenu = bar->addMenu("编辑");
    // 创建菜单项
    QAction *newAction = fileMenu->addAction("新建");
    connect(newAction, &QAction::triggered, this,&notepad::slotNew);
    // 添加分割线
    fileMenu->addSeparator();
    QAction *openAction = fileMenu->addAction("打开");
    connect(openAction, &QAction::triggered, this,&notepad::slotOpen);
    fileMenu->addSeparator();
    QAction *saveAction = fileMenu->addAction("保存");
    connect(saveAction, &QAction::triggered, this,&notepad::slotSave);
    fileMenu->addSeparator();
    QAction *saveAsAction = fileMenu->addAction("另存");
    connect(saveAsAction, &QAction::triggered, this,&notepad::slotSaveAs);
    // 创建编辑
    QAction *repealAction = editMenu->addAction("撤销");
    connect(repealAction, &QAction::triggered, this,&notepad::slotRepeal);
    editMenu->addSeparator();
    QAction *cutAction = editMenu->addAction("剪切");
    connect(cutAction, &QAction::triggered, this,&notepad::slotCut);
    editMenu->addSeparator();
    QAction *copyAction = editMenu->addAction("复制");
    connect(copyAction, &QAction::triggered, this,&notepad::slotCopy);
    editMenu->addSeparator();
    QAction *pasteAction = editMenu->addAction("粘贴");
    connect(pasteAction, &QAction::triggered, this,&notepad::slotPaste);
    editMenu->addSeparator();
    QAction *deleteAction = editMenu->addAction("删除");
    connect(deleteAction, &QAction::triggered, this,&notepad::slotDelete);
    editMenu->addSeparator();
}
bool notepad::isNotNeedSave()
{
    // 判断文本是否发生变化
    if (textEdit->document()->isModified())
    {
        QMessageBox::StandardButton res = QMessageBox::warning(this, "waring", "文本已改变，是否保存",
                                                               QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel, QMessageBox::Save);
        if (res == QMessageBox::Save) // 保存
            return slotSave();
        else if (res == QMessageBox::Discard) // 不保存
            return true;
        else if (res == QMessageBox::Cancel) // 取消
            return false;
    }
    return true;
}
// 设置窗口目前操作的文件
void notepad::setFileName(QString fileName)
{
    curFile = fileName;
    textEdit->document()->setModified(false); // 设置文本状态为未修改
    this->setWindowModified(false);           // 设置窗口状态为未修改
    QFileInfo fileInof(fileName);
    QString fname = fileInof.fileName();
    if (fileName.isEmpty())
    {
        setWindowTitle(tr("新建记事本"));
    }
    else
        setWindowTitle(fname);
}
void notepad::slotNew()
{
    if (isNotNeedSave())
    {
        textEdit->clear();
        setFileName("");
    }
}

void notepad::slotOpen()
{
    if (isNotNeedSave())
    {
        QString fileName = QFileDialog::getOpenFileName(this, "打开文件", "./", "(*.txt)");
        if (!fileName.isEmpty())
        {
            QFile *file = new QFile(fileName);
            if (false == file->open(QFile::ReadOnly | QFile::Text))
            {
                QMessageBox::critical(this, "critical", "文件无法读取");
                delete file;
                return;
            }
            else
            {
                QTextStream in(file);
                textEdit->setText(in.readAll());
                // qDebug()<<fileName;
                setFileName(fileName);
                file->close();
                return;
            }
        }
    }
}
bool notepad::slotSave()
{
    // 判断文件是否存在
    if (curFile.isEmpty())
    {
        // 不存在另存
        return slotSaveAs();
    }
    else
    {
        // 存在就保存
        return slotSaveFile(curFile);
    }
}
bool notepad::slotSaveAs()
{
    // 获取保存的文件名
    QString fileName = QFileDialog::getSaveFileName(this, tr("另存"), ".", tr("Text Files(*.txt)"));
    if (fileName.isEmpty())
        return false;
    else
        return slotSaveFile(fileName); // 创建文件并保存
}
bool notepad::slotSaveFile(QString &fileName)
{
    QFile *file = new QFile(fileName);
    if (false == file->open(QFile::WriteOnly | QFile::Text))
    {
        QMessageBox::critical(this, "critical", "文件无法写入");
        delete file;
        return false;
    }
    else
    {
        QTextStream out(file);
        out << textEdit->toPlainText();
        file->setFileName(fileName);
        file->close();
        return true;
    }
}
// 编辑
void notepad::slotRepeal()
{
    textEdit->undo();
}
void notepad::slotCopy()
{
    textEdit->copy();
}
void notepad::slotPaste()
{
    textEdit->paste();
}
void notepad::slotCut()
{
    textEdit->cut();
}
void notepad::slotDelete()
{
    // 获取光标
    QTextCursor cursor = textEdit->textCursor();
    if (cursor.hasSelection()) // 删除所选区域
        cursor.clearSelection();
    cursor.deletePreviousChar(); // 删除前一个并移动光标
    textEdit->setTextCursor(cursor);
}

void notepad::closeEvent(QCloseEvent *e)
{
    if (isNotNeedSave())
        e->accept(); // 接受关闭事件，允许关闭
    else
        e->ignore(); // 忽略事件，直接关闭
}

notepad::~notepad()
{
    delete ui;
}
